package com.ethan.crm.web.controller;

import com.ethan.crm.domain.Menu;
import com.ethan.crm.service.IMenuService;
import com.ethan.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private IMenuService menuService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/loginPage")
    public String loginPage(){
        return "/login";
    }


    @RequestMapping("/cancel")
    public String cancel(HttpServletRequest request,HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        return "redirect:loginPage";
    }

    @RequestMapping("/queryForMenu")
    @ResponseBody
    public List<Menu> queryForMenu(){
        //每次请求首页都会进行查询，为了减少查询，将系统菜单放到session中
        List<Menu> menuList = (List<Menu>) UserContext.get().getSession().getAttribute(UserContext.MENU_IN_SESSION);
        return menuList;
    }
}
